﻿//
// Created by Freeman on 2022/2/9.
//

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include "../Headers/Sorting.h"

using namespace std;

void PAT_A1025::solve() {
    int nLocations;
    cin >> nLocations;
    vector<Candidate> globalVec;
    for (int location = 1; location <= nLocations; location++) {
        // 读取输入并存储
        int nCandidates;
        cin >> nCandidates;
        vector<Candidate> localVec;
        for (int i = 1; i <= nCandidates; i++) {
            string registrationNumber;
            int score;
            cin >> registrationNumber >> score;
            localVec.emplace_back(registrationNumber, location, score);
        }
        // 局部排序
        sort(localVec.begin(), localVec.end(), Candidate::comp);
        localVec[0].localRank = 1;
        for (int i = 1; i < localVec.size(); i++) {
            if (localVec[i].score < localVec[i - 1].score) localVec[i].localRank = i + 1;
            else localVec[i].localRank = localVec[i - 1].localRank;
        }

        globalVec.insert(globalVec.end(), localVec.begin(), localVec.end());
    }
    // 全局排序
    sort(globalVec.begin(), globalVec.end(), Candidate::comp);
    globalVec[0].finalRank = 1;
    for (int i = 1; i < globalVec.size(); i++) {
        if (globalVec[i].score < globalVec[i - 1].score) globalVec[i].finalRank = i + 1;
        else globalVec[i].finalRank = globalVec[i - 1].finalRank;
    }
    // 输出
    cout << globalVec.size() << endl;
    for (const Candidate &candidate:globalVec) {
        cout << candidate.registrationNumber << " " << candidate.finalRank << " " << candidate.locationNumber << " "
             << candidate.localRank << endl;
    }
}

bool PAT_A1025::Candidate::comp(const PAT_A1025::Candidate &oA, const PAT_A1025::Candidate &oB) {
    if (oA.score > oB.score) return true;
    else if (oA.score == oB.score) return oA.registrationNumber <= oB.registrationNumber;
    else return false;
}

void PAT_A1062::solve() {
    vector<Student> sage, noble, fool, other;
    int nStudents, passingLine, excellentLine;
    cin >> nStudents >> passingLine >> excellentLine;
    // 分类
    for (int i = 1; i <= nStudents; i++) {
        string admissionNumber;
        int virtue, talent;
        cin >> admissionNumber >> virtue >> talent;
        if (virtue >= excellentLine && talent >= excellentLine) { // 圣人
            sage.emplace_back(admissionNumber, virtue, talent);
        }
        else if (virtue < passingLine || talent < passingLine) { // 不合格
            continue;
        }
        else if (virtue >= excellentLine) { // 君子
            noble.emplace_back(admissionNumber, virtue, talent);
        }
        else if (virtue < excellentLine && talent < excellentLine && virtue >= talent) { // 愚人
            fool.emplace_back(admissionNumber, virtue, talent);
        }
        else other.emplace_back(admissionNumber, virtue, talent);
    }
    // 排序
    sort(sage.begin(), sage.end(), Student::comp);
    sort(noble.begin(), noble.end(), Student::comp);
    sort(fool.begin(), fool.end(), Student::comp);
    sort(other.begin(), other.end(), Student::comp);
    // 输出
    int totalPassing = sage.size() + noble.size() + fool.size() + other.size();
    cout << totalPassing << endl;
    for (const Student &stu:sage) {
        cout << stu.admissionNumber << " " << stu.virtueGrade << " " << stu.talentGrade << endl;
    }
    for (const Student &stu:noble) {
        cout << stu.admissionNumber << " " << stu.virtueGrade << " " << stu.talentGrade << endl;
    }
    for (const Student &stu:fool) {
        cout << stu.admissionNumber << " " << stu.virtueGrade << " " << stu.talentGrade << endl;
    }
    for (const Student &stu:other) {
        cout << stu.admissionNumber << " " << stu.virtueGrade << " " << stu.talentGrade << endl;
    }
}

bool PAT_A1062::Student::comp(const PAT_A1062::Student &sA, const PAT_A1062::Student &sB) {
    int totalA = sA.talentGrade + sA.virtueGrade;
    int totalB = sB.talentGrade + sB.virtueGrade;
    if (totalA != totalB) return totalA > totalB;
    else { // 总分相等
        if (sA.virtueGrade != sB.virtueGrade) return sA.virtueGrade > sB.virtueGrade;
        else { // 德才分均相等
            return sA.admissionNumber < sB.admissionNumber;
        }
    }
}

bool PAT_A1012::Rank::comp(const PAT_A1012::Rank &rankA, const PAT_A1012::Rank &rankB) {
    if (rankA.value != rankB.value) return rankA.value < rankB.value;
    else return rankA.priority > rankB.priority;
}

bool PAT_A1012::Student::compByC(const PAT_A1012::Student *stuA, const PAT_A1012::Student *stuB) {
    return stuA->c > stuB->c;
}

bool PAT_A1012::Student::compByMath(const PAT_A1012::Student *stuA, const PAT_A1012::Student *stuB) {
    return stuA->math > stuB->math;
}

bool PAT_A1012::Student::compByEnglish(const PAT_A1012::Student *stuA, const PAT_A1012::Student *stuB) {
    return stuA->english > stuB->english;
}

bool PAT_A1012::Student::compByAvg(const PAT_A1012::Student *stuA, const PAT_A1012::Student *stuB) {
    return stuA->avg > stuB->avg;
}

void PAT_A1012::Student::sortRanks() {
    sort(ranks, ranks + 4, Rank::comp);
}

PAT_A1012::Rank PAT_A1012::Student::getBestRank() {
    return ranks[0];
}

void PAT_A1012::solve() {
    int nStudents, nQueries;
    cin >> nStudents >> nQueries;
    vector<Student *> cList, mathList, englishList, avgList;
    // 读取输入
    for (int i = 1; i <= nStudents; i++) {
        string id;
        int c, math, english;
        cin >> id >> c >> math >> english;
        auto stu = new Student(id, c, math, english);
        cList.push_back(stu);
        mathList.push_back(stu);
        englishList.push_back(stu);
        avgList.push_back(stu);
    }
    // 计算排位
    // 对每个科目排序
    sort(cList.begin(), cList.end(), Student::compByC);
    sort(mathList.begin(), mathList.end(), Student::compByMath);
    sort(englishList.begin(), englishList.end(), Student::compByEnglish);
    sort(avgList.begin(), avgList.end(), Student::compByAvg);
    // 计算每位学生每个科目的排名
    int cRank = 1, mathRank = 1, engRank = 1, avgRank = 1;
    cList[0]->ranks[0].value = cRank;
    mathList[0]->ranks[1].value = mathRank;
    englishList[0]->ranks[2].value = engRank;
    avgList[0]->ranks[3].value = avgRank;
    for (int i = 1; i < nStudents; i++) {
        if (cList[i]->c == cList[i - 1]->c) cList[i]->ranks[0].value = cList[i - 1]->ranks[0].value;
        else cList[i]->ranks[0].value = i + 1;
        if (mathList[i]->math == mathList[i - 1]->math) mathList[i]->ranks[1].value = mathList[i - 1]->ranks[1].value;
        else mathList[i]->ranks[1].value = i + 1;
        if (englishList[i]->english == englishList[i - 1]->english)
            englishList[i]->ranks[2].value = englishList[i - 1]->ranks[2].value;
        else englishList[i]->ranks[2].value = i + 1;
        if (avgList[i]->avg == avgList[i - 1]->avg) avgList[i]->ranks[3].value = avgList[i - 1]->ranks[3].value;
        else avgList[i]->ranks[3].value = i + 1;
    }
    // 找出排名最好的科目
    for (Student *stu:avgList) {
        stu->sortRanks();
    }

    // 输出查询结果
    for (int i = 1; i <= nQueries; i++) {
        string id;
        cin >> id;
        bool found = false;
        for (Student *stu:avgList) {
            if (stu->id == id) {
                Rank bestRank = stu->getBestRank();
                cout << bestRank.value << " " << bestRank.code << endl;
                found = true;
                break;
            }
        }
        if (!found) cout << "N/A" << endl;
    }

    // 释放空间
    for (int i = 0; i < nStudents; i++) delete avgList[i];
}
